<script>
	import WSCoordinate from '@/js_sdk/WSCoordinate'
	import {appletImgUrl,zpImgUrl} from "@/common/config.js"
	export default {
		globalData: {
			bindState: false
		},
		onLaunch: function() {
			// this.upload()
             //   console.log("路径",zpImgUrl)
			this.errNumber = 0
			// console.log('App Launch')
			if (uni.getSystemInfoSync().model.indexOf('iPad') != -1) {
				uni.showModal({
					title: '提示',
					content: '使用移动设备打开中盈甄品体验更加~~',
					confirmText: '我晓得了',
					showCancel: false
				})
			}
			//#ifdef H5
			if(uni.getStorageSync('userToken')){
				getApp().globalData.bindState = true
			}
			// else{
			// 	uni.reLaunch({
			// 		url: '/pages/user/login/loginH5'
			// 	});
			// }
			
			
			//#endif

			//#ifdef MP-WEIXIN
			const self = this
			if (uni.getStorageSync('userToken')) {
				if (uni.getStorageSync('locationPermission')) {
					this.reportingPosition()
					setInterval(this.reportingPosition, 1000 * 60 * 5)
				}
			}
			// 登录
			wx.login({
				success: res => {
					console.log(res);
					let openId = self.$u.post('user/getOpenId', {
						code: res.code,
						xcxName: 'MH_MALL'
					}).then(res => {
						//console.log('微信',res)
						uni.setStorageSync('openId', res.openId)
						getApp().globalData.bindState = res.token ? true : false
						if (res.token) {
							uni.setStorageSync('userToken', {
								id: res.token
							})
							self.updateUserInfo()
						}
					})
				}
			})
			//#endif
			this.getRegionList()
		},
		methods: {
			upload() {
				const self = this
				uni.chooseImage({
					success(res) {
						uni.uploadFile({
							url: self.$u.http.config.baseUrl + '/upload/userPhoto',
							filePath: res.tempFilePaths[0],
							name: 'file',
							async success(res) {
								let data = JSON.parse(res.data)
								if (data.code != 0) {
									uni.showToast({
										title: data.message,
										icon: 'none'
									})
									return
								}
								console.log(self.imageURL + data.data[0].path + '/' + data.data[0].name);
							},
							fail(error) {
								uni.showToast({
									title: '上传失败！',
									icon: 'none'
								})
							}
						})
					}
				})
			},
			getRegionList() {
				// if (uni.getStorageSync('regionList')) return
				this.$u.get('gbarea/listAllArea').then(res => {
					uni.setStorageSync('regionList', res)
				})
			},
			updateUserInfo() {
				const self = this
				self.$u.get('user/info').then(userInfo => {
					if (userInfo.headPhoto) {
						if (userInfo.headPhoto.indexOf("https") != 0) {
							userInfo.headPhoto = this.imageURL + userInfo.headPhoto
						}
					} else {
						userInfo.headPhoto = zpImgUrl+'logo.png'
					}
					uni.setStorageSync('userInfo', userInfo)
				})
			},
			/**
			 * 上报地理位置
			 * 每5分钟上报一次
			 */
			reportingPosition() {
				const self = this
				let BD_Coordinate = WSCoordinate.transformFromGCJToBaidu(uni.getStorageSync('locationPermission').latitude, uni.getStorageSync(
					'locationPermission').longitude)
				self.$u.post('user/location/reportLocation', {
					longitude: BD_Coordinate.longitude,
					latitude: BD_Coordinate.latitude,
					nickName: uni.getStorageSync('userInfo').nickName,
					userPhoto: uni.getStorageSync('userInfo').headPhoto
				}).then(res => {
					uni.setStorageSync('reportingStatus', true)
					self.errNumber = 0
				}).catch(error => {
					// uni.removeStorageSync('locationPermission')
					// uni.removeStorageSync('reportingStatus')
					self.errNumber++
					if (self.errNumber >= 3) return
					this.reportingPosition()
				})
			},
		},
		onShow: function() {

			// console.log('App Show')
			const updateManager = uni.getUpdateManager(); // 获取更新管理器对象
			updateManager.onCheckForUpdate(function(res) {
				if (res.hasUpdate) {
					updateManager.onUpdateReady(function() {
						uni.showModal({
							title: '更新提示',
							content: '新版本已到位，是否现在更新？',
							confirmText: '立即更新',
							cancelText: '暂不更新',
							success: res => {
								if (res.confirm) {
									updateManager.applyUpdate();
								}
							}
						})
					})
					updateManager.onUpdateFailed(function() {
						uni.showModal({
							title: '提示',
							content: '检查到有新版本，但是下载失败，请检查网络设置',
							showCancel: false
						})
					})
				}
			})
			//#ifdef MP-WEIXIN
			if (wx.getLaunchOptionsSync().scene == '1007' || wx.getLaunchOptionsSync().scene == '1008') {
				//	聊天会话中的小程序消息卡片
			}
			//#endif
		},
		onHide: function() {
			// console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";

	view,
	image {
		box-sizing: border-box;
	}

	input {
		caret-color: #FF4E47;
	}

	image {
		vertical-align: middle;
	}

	.uni-swiper-wrapper,
		{
		overflow: visible !important;
	}

	/*每个页面公共css */
</style>
